package com.example.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期操作
 * 
 * @ClassName: DateUtil.java
 * @Description:
 * @author: xiaozhijun
 * @Date: 2016年10月28日 下午2:06:22
 */
public class DateUtil {
	private static final int WEEK_SECONDS = 86400 * 7 * 1000;

	/**
	 * 字符串转日期
	 * 
	 * @author xiaozhijun
	 * @Date: 2016年10月28日 下午2:05:45
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static final Date StringToDate(String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(format);
		return date;
	}
	
	public static final Date StringToDateTime(String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(format);
		return date;
	}

	public static final String DateToString(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = format.format(date);
		return str;
	}

	public static final String DateToString(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		String str = format.format(date);
		return str;
	}

	/**
	 * 按格式输出日期
	 * 
	 * @author xiaozhijun
	 * @Date: 2016年11月30日 下午7:49:01
	 * @param format
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static final Date StringToDate(String format, String pattern) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = sdf.parse(format);
		return date;
	}

	/**
	 * 获取上个月的年+月份 201609 6位数
	 * 
	 * @author xiaozhijun
	 * @Date: 2016年12月5日 下午1:52:39
	 * @return
	 */
	public static final String getUpMonth() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.MONTH, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取今天日期
	 * 
	 * @author xiaozhijun
	 * @Date: 2017年11月15日 下午2:54:06
	 * @return
	 * @throws Exception
	 */
	public static final Date getTodayEndOffset(int offset) throws Exception {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DATE, offset);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		return StringToDate(cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
				+ cal.get(Calendar.DAY_OF_MONTH) + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
	}

	// public static final boolean isToday(Date startDate, Date endDate){
	// if((startDate.getYear() + "-" + startDate.getMonth() + "-" +
	// startDate.getDay()).equals(endDate.getYear() + "-" + endDate.getMonth() +
	// "-" + endDate.getDay())){
	// return true;
	// }
	// return false;
	// }
	//

	public static boolean isSameDay(Date date1, Date date2) {
		if (date1 != null && date2 != null) {
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(date1);
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(date2);
			return isSameDay(cal1, cal2);
		} else {
			return false;
		}
	}

	public static boolean isSameDay(Date date1, long date2) {
		if (date1 != null && date2 != 0) {
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(date1);
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(new Date(date2));
			return isSameDay(cal1, cal2);
		} else {
			return false;
		}
	}

	public static boolean isSameDay(long date1, long date2) {
		if (date1 != 0 && date2 != 0) {
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(new Date(date1));
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(new Date(date2));
			return isSameDay(cal1, cal2);
		} else {
			return false;
		}
	}

	public static boolean isSameDay(Calendar cal1, Calendar cal2) {
		if (cal1 != null && cal2 != null) {
			return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
		} else {
			return false;
		}
	}

	private static final int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
		if (dayOfWeek == 1) {
			return -6;
		} else {
			return 2 - dayOfWeek;
		}
	}

	/**
	 * 获取当前日期的星期1
	 * 
	 * @author xiaozhijun
	 * @Date: 2017年11月23日 下午2:08:11
	 * @return
	 */
	public static final Date getMonday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		try {
			return StringToDate(preMonday + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 查询过去7天的第一天
	 * 
	 * @author xiaozhijun
	 * @Date: 2018年1月2日 上午10:18:54
	 * @return
	 */
	public static final Date getLast7DayStart() {
		Date current = new Date();
		Date startTime = new Date(current.getTime() - WEEK_SECONDS);
		startTime.setHours(0);
		startTime.setMinutes(0);
		startTime.setSeconds(0);
		return startTime;
	}

	public static final Date getSunDay() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
		Date sunDay = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		try {
			return StringToDate(df.format(sunDay) + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取过去n天的日期
	 * 
	 * @param days
	 * @return
	 */
	public static Date getDayBefore(Integer days) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - days);
		return c.getTime();
	}

	/**
	 * 获取unix时间戳（秒级）
	 * 
	 * @param date
	 * @return Long
	 * @author liuhuaqing
	 * @date 2018年8月30日下午4:01:37
	 */
	public static Long getUnixTimestamp(Date date) {
		return date.getTime() / 1000;
	}

	/**
	 * 从unix时间戳获取时间date
	 * 
	 * @param stamp
	 * @return Date
	 * @author liuhuaqing
	 * @date 2018年8月30日下午4:01:57
	 */
	public static Date getDateFromUnitTimestamp(Long stamp) {
		return new Date(stamp * 1000);
	}

	/**
	 * 获取指定日期的时间戳范围
	 * 
	 * @param date
	 * @return Long[] 下标0为 当天开始的时间戳，下标 1 为当天结束的时间戳
	 * @author liuhuaqing
	 * @date 2018年8月31日上午11:11:19
	 */
	public static Long[] getRangeTimeStamp(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		Date start = c.getTime();

		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		Date end = c.getTime();

		return new Long[] { start.getTime() / 1000, end.getTime() / 1000 };
	}

	/**
	 * 时间显示成xx分钟之前
	 *
	 * @author tcf
	 * @date 2018年12月14日 上午9:59:07
	 * @param stamp
	 * @return
	 */
	public static String getString(Date date) {
		if(date == null){
			return null;
		}
		String s=null;
		long num = 0;
		Date now = new Date();
		if(date.after(now)){
			return null;
		}
		System.out.println(DateToString(now) + "  :  " + DateToString(date));
		long timeDiff = (now.getTime()-date.getTime()) /1000;
		System.out.println(timeDiff);
		//一分钟之内
		if(timeDiff<60){
			s = "刚刚";
		}//一分钟到一个小时之间
		else if(60 <= timeDiff &&  timeDiff< 60*60){
			num = timeDiff/(60);
			s = num + "分钟之前";
		}else if(60*60 <= timeDiff && timeDiff< (long)(24*60*60)){
			num = timeDiff/(60*60);
			s = num + "小时之前";
		}else if(24*60*60 <= timeDiff && timeDiff< (long)(30*24*60*60)){
			num = timeDiff/(24*60*60);
			s = num + "天之前";
		}else if((long)(30*24*60*60) <= timeDiff && timeDiff< (long)(12*30*24*60*60)){
			num = timeDiff/(30*24*60*60);
			s = num + "月之前";
		}else{
			num = timeDiff/(12*30*24*60*60);
			s =num +  "年之前";
		}
		return  s;
	}
	
	public static void main(String[] args) throws ParseException {
		Date date = StringToDateTime("2018-02-13 13:31:00");
		String s = getString(date);
		System.out.println(s);
	}

}
